<template>
	
	<view>
		<uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
		<view class="uni-group">
		  <view class="uni-title"></view>
		  <view class="uni-sub-title"></view>
		</view>
		<view class="optimize-box">
			<text>优化店铺的作用是更新店铺对应的主图，系统会自动从该店铺商品下选一张主图作为店铺展示图片。</text>
			<button type="primary" :loading="loading" :disabled="disabled" @click="startOptimize">{{text}}</button>
			<view style="width:100%;line-height:3rem;margin-top:2rem;color:#666;margin-bottom:5rem;font-size:.9rem;text-align: center;">
				{{updateData}} 共更新<text style="color:red;">{{updateCount}}</text>条记录
			</view>
			
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				updateCount:0,//更新总条数
				updateData:'',//更新提示数据
				loading:false,
				text:'开始优化',
				disabled:false,
				shop:[]
			}
		},
		methods: {
			//开始优化
			startOptimize(){
				this.shop = []; 
				this.loading = true;
				this.disabled = true;
				//查询出所有的店铺
				const db = uniCloud.database();
				db.collection('shop').count().then(res=>{
					const count = res.result.total;
					const page = Math.ceil(count/20);
					var c = 0;
					//查询出商品列表
					for(var i = 1;i<=page;i++){
						const skip = (i-1) * 20;
						db.collection('shop').skip(skip).limit(20).field('_id,images,categories').get().then(result=>{
							var data = result.result.data;
							for(var x = 0;x < data.length;x++){
								
								if(data[x].categories=='654f2261466d4107c47c6937' || data[x].categories=='654f240e8b0da47be1cc6f22'){
									var limit = 20;
								}else{
									var limit = 4;
								}
								//随机查询一张店铺对应的商品主图作为店铺图片
								db.collection('goods').where({shop_id:data[x]._id}).orderBy('is_index desc,total_sales desc').field('shop_id,name,price,other_price,images').skip(0).limit(limit).get().then(g=>{
									var view = Math.round(Math.random() * 10000+2000);
									//更新店铺主图
									var image = [];
									for(var j=0;j<g.result.data.length;j++){
										const url = g.result.data[j].images[0].url;
										const title = g.result.data[j].name;
										const price = g.result.data[j].price;
										const old_price = g.result.data[j].other_price;
										image.push({
											url:url,
											title:title,
											price:price,
											old_price:old_price
										})
									}
									var shopId = g.result.data[0].shop_id;
									
									db.collection('shop').where({_id:shopId}).update({
										image:image,
										page_view:view
									}).then(u=>{
										this.updateData = '店铺'+shopId+' 更新成功';
										this.updateCount++;
									}).catch(e=>{
										console.log(e);
									});
									
								});
								
							}
							
							
						});
					}
					
					this.loading = false;
					this.disabled = false;
					
				});
			}
		
		// startGoodsOptimize(){
		// 	//查询出所有的商品
		// 	var db = uniCloud.database();
		// 	db.collection('shop').skip(0).limit(1000).get().then(result=>{
		// 		var data = result.result.data;
		// 		for(var i = 0;i<data.length;i++){
		// 			var id = data[i]._id;
		// 			//查询出每个店铺的商品销量总和
		// 			db.collection('goods').where({shop_id:id}).field('_id,shop_id,total_sales').get().then(res=>{
		// 				var goods = res.result.data;
		// 				var sales = 0;
		// 				for(var x = 0;x<goods.length;x++){
		// 					sales +=goods[x].total_sales;
		// 				}
						
		// 				var shopId = goods[0].shop_id;
		// 				db.collection('shop').where({_id:shopId}).update({	
		// 				total_sales:sales
		// 				}).then(s=>{
		// 					console.log(id+'更新成功');
		// 				});
		// 			});
		// 		}
		// 	});
		// }
	}
	}
</script>

<style scoped>
	.optimize-box button{margin-top:5rem;}
	.optimize-box text{line-height:1.5rem;margin-top:3rem;color:#999;}
	.optimize-box{width:50%;margin:auto;padding-top:5rem;}
</style>
